package com.niannian.adapter;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import com.niannian.MyApplication;
import com.niannian.R;
import com.niannian.activity.FamilyMemberDetailActivity;
import com.niannian.activity.MyInfoEditActivity;
import com.niannian.activity.PushRegardsActivity;
import com.niannian.activity.RegardsActivity;
import com.niannian.bean.GreetingNewsBean;
import com.niannian.bean.MyFamilyMemberBean;
import com.niannian.db.MyDBUser;
import com.niannian.dialog.YesNoDialog2;
import com.niannian.dialog.YesNoDialog2.onSimpleOnCancelListener;
import com.niannian.dialog.YesNoDialog2.onSimpleOnClicklListener;
import com.niannian.util.AsyncHandle;
import com.niannian.util.Common;
import com.niannian.util.ErrorCode;
import com.niannian.util.PlayerManager;
import com.niannian.util.UmengPage;
import com.niannian.util.UserInfoManager;
import com.niannian.util.VAR;
import com.niannian.view.CircleImageView;
import com.niannian.web.nnSyncApi;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 获取问候的adapter
 * 
 * @author shenglin
 * 
 */
public class RegardsReceiveAdaper extends BaseAdapter {
	protected MyDBUser userInfoManager = UserInfoManager.getUserInfoInstance();
	private ArrayList<GreetingNewsBean> fdatas;
	private LayoutInflater inflater;
	private Activity mActivity;

	private Handler handler;
	BaseAdapter adapter;

	public RegardsReceiveAdaper(Activity activity, Handler handler) {
		super();
		this.mActivity = activity;
		this.handler = handler;
		inflater = LayoutInflater.from(mActivity);
		this.fdatas = new ArrayList<GreetingNewsBean>();
	}

	@Override
	public int getCount() {
		return fdatas.size();
	}

	@Override
	public Object getItem(int arg0) {
		return fdatas.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	public ArrayList<GreetingNewsBean> getFDatasList() {
		return fdatas;
	}

	public void setAdapter(BaseAdapter adapter) {
		this.adapter = adapter;
	}

	@Override
	public View getView(final int position, View view, ViewGroup arg2) {
		ViewHolder holder = null;
		if (view == null) {
			view = inflater.inflate(R.layout.item_regardss, null);
			holder = new ViewHolder();

			holder.ll_itme_head = (LinearLayout) view.findViewById(R.id.ll_itme_head);

			holder.ll_itme_head_1 = (LinearLayout) view.findViewById(R.id.ll_itme_head_1);
			holder.riv_avatar_1 = (CircleImageView) view.findViewById(R.id.riv_avatar_1);
			holder.tv_user_name_1 = (TextView) view.findViewById(R.id.tv_user_name_1);
			holder.tv_time_1 = (TextView) view.findViewById(R.id.tv_time_1);
			holder.tv_info_1 = (TextView) view.findViewById(R.id.tv_info_1);

			holder.tv_is_play = (TextView) view.findViewById(R.id.tv_is_play);

			holder.pb_fn_loading_1 = (ProgressBar) view.findViewById(R.id.pb_fn_loading_1);
			holder.tv_aud_len_1 = (TextView) view.findViewById(R.id.tv_aud_len_1);
			holder.btn_play_1 = (RelativeLayout) view.findViewById(R.id.btn_play_1);
			holder.iv_status_play_1 = (ImageView) view.findViewById(R.id.iv_status_play_1);

			holder.tv_reply_bottom = (TextView) view.findViewById(R.id.tv_reply_bottom);

			holder.ll_itme_head_2 = (LinearLayout) view.findViewById(R.id.ll_itme_head_2);
			holder.riv_avatar_2 = (CircleImageView) view.findViewById(R.id.riv_avatar_2);
			holder.tv_user_name_2 = (TextView) view.findViewById(R.id.tv_user_name_2);
			holder.tv_time_2 = (TextView) view.findViewById(R.id.tv_time_2);
			holder.tv_info_2 = (TextView) view.findViewById(R.id.tv_info_2);

			holder.pb_fn_loading_2 = (ProgressBar) view.findViewById(R.id.pb_fn_loading_2);
			holder.tv_aud_len_2 = (TextView) view.findViewById(R.id.tv_aud_len_2);
			holder.btn_play_2 = (RelativeLayout) view.findViewById(R.id.btn_play_2);
			holder.iv_status_play_2 = (ImageView) view.findViewById(R.id.iv_status_play_2);

			holder.tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
			holder.tv_afresh_edit = (TextView) view.findViewById(R.id.tv_afresh_edit);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		final GreetingNewsBean fdata = fdatas.get(position);

		if (!Common.empty(fdata.getAuthor_innername())) {
			holder.tv_user_name_1.setText(fdata.getAuthor_innername());
		} else {
			holder.tv_user_name_1.setText(fdata.getAuthor_nickname());
		}

		// 播放视图显示
		if (fdata.getAud_len() > 0) {
			holder.btn_play_1.setVisibility(View.VISIBLE);
			holder.tv_aud_len_1.setText(Common.showAudLen(fdata.getAud_len()));
			if (PlayerManager.getAudioPlayer(mActivity).currPlayingPath.equals(fdata.getAud())) {
				if (PlayerManager.getAudioPlayer(mActivity).isPlayStatus == 1) {
					holder.iv_status_play_1.setBackgroundResource(R.drawable.family_status_play_anim);
					AnimationDrawable anim = (AnimationDrawable) holder.iv_status_play_1.getBackground();
					anim.stop();
					anim.start();
				} else if (PlayerManager.getAudioPlayer(mActivity).isPlayStatus == 2) {
					holder.iv_status_play_1.setBackgroundResource(R.drawable.status_pause);
				} else {
					holder.iv_status_play_1.setBackgroundResource(R.drawable.play_3);
				}
			} else {
				holder.iv_status_play_1.setBackgroundResource(R.drawable.play_3);
			}
		} else {
			holder.btn_play_1.setVisibility(View.GONE);
		}

		MyApplication.finalbitmap.display(holder.riv_avatar_1, Common.getAvatarThumbs(fdata.getAuthor_avatar()));
		holder.tv_time_1.setText(Common.sgmdate3(fdata.getSent_time(), null, null));

		if (!Common.empty(fdata.getInfo())) {
			holder.tv_info_1.setVisibility(View.VISIBLE);
			holder.tv_info_1.setText(fdata.getInfo());
		} else {
			holder.tv_info_1.setVisibility(View.GONE);
		}

		if (PlayerManager.getAudioPlayer(mActivity).DownloadingMap.containsKey(fdata.getAud())) {
			holder.pb_fn_loading_1.setVisibility(View.VISIBLE);
		} else {
			holder.pb_fn_loading_1.setVisibility(View.GONE);
		}

		holder.btn_play_1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (System.currentTimeMillis() - VAR.ExitTime > VAR.INTERVAL) {
					PlayerManager.getAudioPlayer(mActivity).aPlayer(adapter, fdata.getAud());
					VAR.ExitTime = System.currentTimeMillis();

					if ("false".equals(fdata.getIs_checked())) {
						Map<String, Object> paramMap = new HashMap<String, Object>();
						paramMap.put("id", fdata.getId());
						new updataIsCheched().init(paramMap).execute();
						fdata.setIs_checked("true");
						notifyDataSetChanged();
					}

				}
			}
		});

		holder.riv_avatar_1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				userOnc(fdata, position);
			}
		});
		holder.tv_user_name_1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				userOnc(fdata, position);
			}
		});
		holder.ll_itme_head.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
			}
		});

		if (fdata.getAud_len() > 0 && "false".equals(fdata.getIs_checked())) {
			holder.tv_is_play.setVisibility(View.VISIBLE);
		} else {
			holder.tv_is_play.setVisibility(View.GONE);
		}

		if (!Common.empty(fdata.getGreetingNewsBean2())) {
			holder.ll_itme_head_2.setVisibility(View.VISIBLE);
			holder.tv_reply_bottom.setVisibility(View.GONE);
			final GreetingNewsBean fdata2 = fdatas.get(position).getGreetingNewsBean2();

			if (!Common.empty(fdata2.getAuthor_innername())) {
				holder.tv_user_name_2.setText(fdata2.getAuthor_innername());
			} else {
				holder.tv_user_name_2.setText(fdata2.getAuthor_nickname());
			}
			int curtime = (int) ((new Date()).getTime() / 1000);
			int time = (Integer) curtime - fdata2.getSent_time();
			if (time < 0) {
				holder.tv_cancel.setVisibility(View.VISIBLE);
				holder.tv_afresh_edit.setVisibility(View.VISIBLE);
			} else {
				holder.tv_cancel.setVisibility(View.GONE);
				holder.tv_afresh_edit.setVisibility(View.GONE);
			}
			// 播放视图显示
			if (fdata2.getAud_len() > 0) {
				holder.btn_play_2.setVisibility(View.VISIBLE);
				holder.tv_aud_len_2.setText(Common.showAudLen(fdata2.getAud_len()));
				if (PlayerManager.getAudioPlayer(mActivity).currPlayingPath.equals(fdata2.getAud())) {
					if (PlayerManager.getAudioPlayer(mActivity).isPlayStatus == 1) {
						holder.iv_status_play_2.setBackgroundResource(R.anim.play_anim);
						AnimationDrawable anim = (AnimationDrawable) holder.iv_status_play_2.getBackground();
						anim.stop();
						anim.start();
					} else if (PlayerManager.getAudioPlayer(mActivity).isPlayStatus == 2) {
						holder.iv_status_play_2.setBackgroundResource(R.drawable.status_pause2);
					} else {
						holder.iv_status_play_2.setBackgroundResource(R.drawable.play_3_w);
					}
				} else {
					holder.iv_status_play_2.setBackgroundResource(R.drawable.play_3_w);
				}
			} else {
				holder.btn_play_2.setVisibility(View.GONE);
			}

			MyApplication.finalbitmap.display(holder.riv_avatar_2, Common.getAvatarThumbs(fdata2.getAuthor_avatar()));
			holder.tv_time_2.setText(Common.sgmdate3(fdata2.getSent_time(), null, null));

			if (!Common.empty(fdata2.getInfo())) {
				holder.tv_info_2.setVisibility(View.VISIBLE);
				holder.tv_info_2.setText(fdata2.getInfo());
			} else {
				holder.tv_info_2.setVisibility(View.GONE);
			}

			if (PlayerManager.getAudioPlayer(mActivity).DownloadingMap.containsKey(fdata2.getAud())) {
				holder.pb_fn_loading_2.setVisibility(View.VISIBLE);
			} else {
				holder.pb_fn_loading_2.setVisibility(View.GONE);
			}

			holder.btn_play_2.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					if (System.currentTimeMillis() - VAR.ExitTime > VAR.INTERVAL) {
						PlayerManager.getAudioPlayer(mActivity).aPlayer(adapter, fdata2.getAud());
						VAR.ExitTime = System.currentTimeMillis();
					}
				}
			});

			holder.riv_avatar_2.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					userOnc(fdata2, position);
				}
			});
			holder.tv_user_name_2.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					userOnc(fdata2, position);
				}
			});
			holder.ll_itme_head_2.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
				}
			});

			holder.tv_afresh_edit.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub

					Bundle pBundle = new Bundle();
					pBundle.putSerializable("fdata", fdata2);
					pBundle.putInt("from", PushRegardsActivity.FROM_EDIT_REGARDS);
					Intent data = new Intent(mActivity, PushRegardsActivity.class);
					data.putExtras(pBundle);
					mActivity.startActivityForResult(data, PushRegardsActivity.FROM_EDIT_REGARDS);
				}
			});
			holder.tv_cancel.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					curView = (View) v.getParent().getParent();
					initDialog(fdata2.getId());
				}
			});

			if (fdata2.getIsdelete() == 1 && curView != null) {
				removeListItem(curView, position);
			}

		} else {
			holder.ll_itme_head_2.setVisibility(View.GONE);
			holder.tv_reply_bottom.setVisibility(View.VISIBLE);
			holder.tv_reply_bottom.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mActivity, PushRegardsActivity.class);
					intent.putExtra("avatar", fdata.getAuthor_avatar());
					intent.putExtra("uid", fdata.getAuthor());
					intent.putExtra("reply_to", fdata.getId());

					intent.putExtra("from", PushRegardsActivity.FROM_PUSH_REGARDS_CONFIRM_REPY);
					mActivity.startActivityForResult(intent, PushRegardsActivity.FROM_PUSH_REGARDS_CONFIRM_REPY);
					mActivity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
					MobclickAgent.onEvent(mActivity, UmengPage.reply_add_regard);
				}
			});
		}

		return view;

	}

	private void userOnc(GreetingNewsBean fdata, int position) {

		if (userInfoManager.id == fdata.getAuthor()) {
			Intent intent = new Intent(mActivity, MyInfoEditActivity.class);
			mActivity.startActivity(intent);
			mActivity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
		} else {
			MyFamilyMemberBean fd = new MyFamilyMemberBean();
			fd.setId(fdata.getAuthor());
			// fd.setFamily(fdata.getFamily());
			fd.setNickname(fdata.getAuthor_nickname());
			fd.setAvatar(fdata.getAuthor_avatar());

			Intent intent = new Intent(mActivity, FamilyMemberDetailActivity.class);
			intent.putExtra("position", position);
			intent.putExtra("data", fd);
			mActivity.startActivity(intent);
			mActivity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
		}

	}

	View curView;
	// 删除动态提示
	YesNoDialog2 tip;

	private void initDialog(final int id) {
		tip = new YesNoDialog2(mActivity, "确认要撤销吗?");
		tip.setOnCancelListener(new onSimpleOnCancelListener() {

			@Override
			public void onCancel() {
				tip.dismiss();
			}
		});
		tip.setOnConfimClickListener(new onSimpleOnClicklListener() {

			@Override
			public void onClick() {
				Message msg = new Message();
				msg.what = RegardsActivity.DELETE_SEND_REGARDS;
				msg.arg1 = id;

				handler.sendMessage(msg);
				tip.dismiss();
				// removeListItem(curView, position);
			}
		});
		tip.show();
	}

	protected void removeListItem(View rowView, final int positon) {
		final Animation animation = (Animation) AnimationUtils.loadAnimation(rowView.getContext(), R.anim.item_anim);
		animation.setAnimationListener(new AnimationListener() {
			public void onAnimationStart(Animation animation) {
			}

			public void onAnimationRepeat(Animation animation) {
			}

			public void onAnimationEnd(Animation animation) {

				fdatas.get(positon).setGreetingNewsBean2(null);
				notifyDataSetChanged();
				animation.cancel();
			}
		});

		rowView.startAnimation(animation);
	}

	protected class updataIsCheched extends AsyncHandle {

		protected void errorFinally(Map<String, Object> paramMap) {

		}

		protected void handleData(JSONObject paramJSONObject, Map<String, Object> paramMap) throws JSONException {
			int retCode = paramJSONObject.getInt("code");
			if (retCode == ErrorCode.OK) {
				try {
				} catch (Exception e) {
					// TODO: handle exception
				}
			} else {
				Common.tip(mActivity, paramJSONObject.getString("msg"));
			}
		}

		protected String runTask(Map<String, Object> paramMap) {
			return nnSyncApi.checkRegard((Integer) paramMap.get("id"));
		}
	}

	class ViewHolder {
		LinearLayout ll_itme_head;
		LinearLayout ll_itme_head_1;
		CircleImageView riv_avatar_1;
		TextView tv_user_name_1;
		TextView tv_time_1;
		TextView tv_info_1;

		TextView tv_is_play;

		ProgressBar pb_fn_loading_1;
		TextView tv_aud_len_1;
		RelativeLayout btn_play_1;
		ImageView iv_status_play_1;

		TextView tv_reply_bottom;

		LinearLayout ll_itme_head_2;
		CircleImageView riv_avatar_2;
		TextView tv_user_name_2;
		TextView tv_time_2;
		TextView tv_info_2;

		ProgressBar pb_fn_loading_2;
		TextView tv_aud_len_2;
		RelativeLayout btn_play_2;
		ImageView iv_status_play_2;

		TextView tv_afresh_edit;
		TextView tv_cancel;

	}

}
